/*
 * Copyright (c) 2010 Mathew Hall, University of Sheffield.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * Neither the name of the University of Sheffield nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package primitives.cluster;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import primitives.graph.Graph;
import primitives.graph.Node;
import primitives.graph.Transition;

public class ClusterHead extends ClusterNode implements Serializable {

    private static final long serialVersionUID = -9201394082812065893L;

    public ClusterHead(Graph g) {
        this(g.getNodes());

    }

    public ClusterHead() {
    }

    public ClusterHead(Set<Node> s) {
        super();
        int count = 0;
        this.setID(count);
        for (Node n : s) {
            ClusterNode cn = new ClusterNode();
            cn.setID(++count);
            cn.addNode(n);
            clusters.add(cn);
        }
    }

    public ClusterHead(Set<IClusterLevel> ch, boolean IGNORED) {
        setChildren(ch);
    }

    public ClusterHead deepCopy() {
        ClusterHead newMe = new ClusterHead(new Graph());
        for (IClusterLevel c : newMe.getChildren()) {
            newMe.deleteChild(c);
        }

        Set<Node> oldNodes = getNodes();

        Set<Transition> oldTransitions = new HashSet<Transition>();


        for (Node n : getNodes()) {
            oldTransitions.addAll(n.getTransitionsAsT());
        }


        for (IClusterLevel c : clusters) {
            newMe.addChild(c.deepCopy());
        }

        for (Integer i : this.getIDs()) {
            newMe.setID(i);
        }

        HashMap<Node, Node> oldToNew = new HashMap<Node, Node>();

        for (Node oldN : getNodes()) {
            for (Node newN : newMe.getNodes()) {
                if (newN.getLabel().equals(oldN.getLabel())) {
                    oldToNew.put(oldN, newN);
                }
            }
        }

        for (Transition t : oldTransitions) {
            Node from = oldToNew.get(t.getSourceNode());

            Node to = oldToNew.get(t.getDestinationNode());

            from.connect(to, t.getLabel());

        }

        return newMe;
    }
}
